const axios = require('axios')

const GITHUB_BASE_URL = 'https://api.github.com'

const isServer = typeof window === 'undefined'

// 服务端环境需要手动拼接url
async function requestGithub (method, url, data, headers) {
  if (!url) {
    throw new Error('url must be provided')
  }
  const result = await axios({
    method,
    url: `${GITHUB_BASE_URL}${url}`,
    data,
    headers,
  })

  return result
}

async function request ({ method = 'GET', url, data = {}, }, req, res, ) {
  // console.log('req,', req.session)
  if (!url) {
    throw Error('没有请求网址')
  }
  console.log(isServer)
  /* if (isServer) {
    const session = req.session
    const githubAuth = session.githubAuth || {}
    const { access_token, token_type } = githubAuth || {}
    const headers = {}
    if (access_token) {
      headers.Authorization = `${token_type} ${access_token}`
    }
    // 服务端走requestGithub方法，
    // 补全api的前缀
    const serverResult = await requestGithub(method, url, data, headers)
    return serverResult
  } */

  if (isServer) {

    const session = req.session
    const githubAuth = session.githubAuth || {}
    const headers = {}


    if (githubAuth.access_token) {
      headers['Authorization'] = `${githubAuth.token_type} ${githubAuth.access_token}`
    }
    return await requestGithub(method, url, data, headers)
  }



  // 客户端需要拼接github前缀 让koa的server可以识别并代理
  const clientResult = await axios({
    method,
    url: `/github${url}`,
    data,
  })
  return clientResult
}

module.exports = {
  request,
  requestGithub,
}




/* const axios = require('axios')

const github_base_url = 'https://api.github.com'
async function requestGithub (method, url, data, headers) {
  return await axios({
    method,
    url: `${github_base_url}${url}`,
    data,
    headers
  })
}

const isServer = typeof window === 'undefined'
async function request ({ method = 'GET', url, data = {} }, req, res) {
  if (!url) {
    throw Error('没有请求网址')
  }
  if (isServer) {
    const session = req.session
    const githubAuth = session.githubAuth || {}
    const headers = {}


    if (githubAuth.access_token) {
      headers['Authorization'] = `${githubAuth.token_type} ${githubAuth.access_token}`
    }
    return await requestGithub(method, url, data, headers)
  } else {
    return await axios({
      method,
      url: `/github${url}`,
      data
    })
  }
}


module.exports = {
  request,
  requestGithub
} */

